Write a python program to check if a string is an anagram of another string.
Write a program to check if a string is an anagram of another string.
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
19-Jun-2023Sure, here is a Python program to check if a string is an anagram of another string:
Python
This program works by first checking if the two strings have the same length. If they do not have the same length, then the two strings cannot be anagrams.
Then, the program creates a dictionary to count the number of occurrences of each character in the first string. The program then iterates through the second string and decrements the count of each character in the dictionary. If the count of a character reaches 0, then the character is no longer present in the second string.
Finally, the program checks if all the counts in the dictionary are 0. If they are, then the two strings are anagrams. Otherwise, the two strings are not anagrams.
To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as
is_anagram.py, you can run it by typing the following command into the command line:Code snippet
This will print the boolean value of whether the two strings are anagrams to the console.
Here is an example of the output of the program:
Code snippet
As you can see, the output of the program is True, which means that the two strings are anagrams.